package classwork.dao;

import classwork.bean.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class StudentDao {
    @Test
    public void selectAll() throws Exception {
        //1.加载配置文件
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        //2.获取工厂对象
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
        //3.获取sqlSession对象
        SqlSession sqlSession = ssf.openSession();
        //4.执行配置文件sql
        List<Student> list = sqlSession.selectList("StudentMapper.selectAll");
        //5.处理结果
        for (Student stu : list) {
            System.out.println(stu);
        }
        //6.释放
        sqlSession.close();
        is.close();
    }

    @Test
    public void selectsAll() throws Exception {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = ssf.openSession();
        List<Student> list = sqlSession.selectList("StudentMapper.selectAll");
        for (Student stu : list) {
            System.out.println(stu);
        }
        sqlSession.close();
        is.close();
    }

    @Test
    public void selectById() throws Exception {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        Student stu = sqlSession.selectOne("StudentMapper.selectById", 3);

        System.out.println(stu);

        sqlSession.close();
        is.close();

    }

    @Test
    public void insert() throws Exception {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);

        SqlSession sqlSession = ssf.openSession();

        Student stu = new Student(6,"凯撒",30);
        sqlSession.insert("StudentMapper.insert",stu);

        sqlSession.commit();
        System.out.println(stu);

        sqlSession.close();
        is.close();
    }

    @Test
    public void update() throws Exception {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        SqlSession sqlSession = sqlSessionFactory.openSession();

       Student stu =new Student(4,"天才",33);
        int i = sqlSession.update("StudentMapper.update", stu);

        sqlSession.commit();
        System.out.println(i);

        sqlSession.close();
        is.close();

    }

    @Test
    public void delete() throws Exception {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        SqlSession sqlSession = sqlSessionFactory.openSession();

        int result = sqlSession.delete("StudentMapper.delete", 4);

        sqlSession.commit();

        System.out.println(result);
        sqlSession.close();
        is.close();
    }
}
